﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FriendlyFire;
using System.IO;

namespace FriendlyFire.UserControls
{
    public partial class PlayerControls : UserControl
    {
        private Search searchCtrl;
        private Video videoCtrl;
        private FriendlyFire myParent;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="s">Search tab reference</param>
        /// <param name="v">Video tab reference</param>
        /// <param name="ffHelper">Friendly Fire helper reference</param>
        public PlayerControls(ref Search s, ref Video v, ref Helper ffHelper, FriendlyFire parent)
        {
            myParent = parent;
            InitializeComponent();
            searchCtrl = s;
            videoCtrl = v;
        }

        #region Accessors
        public Search SearchCtrl
        {
            get { return searchCtrl; }
            set { searchCtrl = value; }
        }

        public Video VideoCtrl
        {
            get { return videoCtrl; }
            set { videoCtrl = value; }
        }
        #endregion


        #region Basic Playlist Buttons
        /// <summary>
        /// Event fire when play button is clicked
        /// </summary>
        private void playButton_Click(object sender, EventArgs e)
        {
            VideoCtrl.vactx.playlist.play();
            videoCtrl.vactx.Visible = true;
            vidTimer.Start();
        }

        /// <summary>
        /// Event fired when pause button is clicked
        /// </summary>
        private void pauseButton_Click(object sender, EventArgs e)
        {
            try { VideoCtrl.vactx.playlist.togglePause(); }
            catch (Exception err) { }
        }

        /// <summary>
        /// Event fire when stop button is clicked
        /// </summary>
        private void stopButton_Click(object sender, EventArgs e)
        {
            VideoCtrl.vactx.playlist.stop();
            videoCtrl.vactx.Visible = false;
            vidTimer.Stop();
        }

        /// <summary>
        /// Event fired when the volume slider is moved, sets the player volume between 0 and 400 percent of default output
        /// </summary>
        private void volTrack_Scroll(object sender, EventArgs e)
        {
            //libvlc_exception_struct ex = new libvlc_exception_struct();
            //VideoCtrl.Libvlc_media_player.audio_set_volume(VideoCtrl.Libvlc_core.Handle, , ref ex);.
            int i = volTrack.Value;
            VideoCtrl.vactx.audio.Volume = i;
        }

        /// <summary>
        /// Event fired when the video tracker is manually adjusted, if the video can be seeked it moves the player position accordingly
        /// </summary>
        private void vidTrack_Scroll(object sender, EventArgs e)
        {
            // VideoCtrl.vactx.input.Length
            // divide by 1000 to get total seconds.
            // divde by 60 to get minutes and decimal seconds.
            try { VideoCtrl.vactx.input.Time = (VideoCtrl.vactx.input.Length / 100) * vidTrack.Value; }
            catch { }
        }

        //TODO the ceiling method causes the video to end prematurely. 100% is reached too soon. Come up with a better method.
        /// <summary>
        /// Event fired every tick of the timer, updates the video trackbar to properly represent the current position of the video
        /// </summary>
        private void vidTimer_Tick(object sender, EventArgs e)
        {
            try { vidTrack.Value = (int)Math.Ceiling((VideoCtrl.vactx.input.Position * 100)); }
            catch { }
        }

        private void backButton_Click(object sender, EventArgs e)
        {
            if (SearchCtrl.playbox.SelectedIndex > 0)
            {
                VideoCtrl.vactx.playlist.clear();
                VideoCtrl.vactx.Visible = true;
                int id = VideoCtrl.vactx.playlist.add(((QueueItem)SearchCtrl.playbox.Items[SearchCtrl.playbox.SelectedIndex - 1]).Dir +
                                "\\" + ((QueueItem)SearchCtrl.playbox.Items[SearchCtrl.playbox.SelectedIndex - 1]).Name,
                                ((QueueItem)SearchCtrl.playbox.Items[SearchCtrl.playbox.SelectedIndex - 1]).Name, null);
                VideoCtrl.vactx.playlist.playItem(id);
                SearchCtrl.playbox.SelectedIndex = SearchCtrl.playbox.SelectedIndex - 1;
                myParent.tabControl1.SelectedTab = myParent.tabControl1.TabPages[4];
            }
        }

        private void fwdButton_Click(object sender, EventArgs e)
        {
            if (SearchCtrl.playbox.SelectedIndex < SearchCtrl.playbox.Items.Count - 1)
            {
                VideoCtrl.vactx.playlist.clear();
                VideoCtrl.vactx.Visible = true;
                int id = VideoCtrl.vactx.playlist.add(((QueueItem)SearchCtrl.playbox.Items[SearchCtrl.playbox.SelectedIndex + 1]).Dir +
                                "\\" + ((QueueItem)SearchCtrl.playbox.Items[SearchCtrl.playbox.SelectedIndex + 1]).Name,
                                ((QueueItem)SearchCtrl.playbox.Items[SearchCtrl.playbox.SelectedIndex + 1]).Name, null);
                VideoCtrl.vactx.playlist.playItem(id);
                SearchCtrl.playbox.SelectedIndex = SearchCtrl.playbox.SelectedIndex + 1;
                myParent.tabControl1.SelectedTab = myParent.tabControl1.TabPages[4];
            }
        }


        /// <summary>
        /// Mute/unmute the VLC activex control. Change the 
        /// icon as necessary as well.
        /// </summary>
        private void audButton_Click(object sender, EventArgs e)
        {
            VideoCtrl.vactx.audio.mute = (VideoCtrl.vactx.audio.mute) ? false : true;
            audButton.Image = (VideoCtrl.vactx.audio.mute) ?
                Image.FromFile(@"icons\player_volume_mute-32.PNG") :
                Image.FromFile(@"icons\player_volume-32.PNG");
        }
        #endregion

        private void vidTrack_ValueChanged(object sender, EventArgs e)
        {
            if (vidTrack.Value.Equals(100))
            {
                // Pretend the next button was clicked
                fwdButton_Click(null, null);
            }
        }
    }
}